package com.kq.xtoolkit.utils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Vibrator;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class XUtils {
    public final static boolean DEBUG = false;
    private static Camera mCamera;
    public final static String TAG = "XToolKit";
     // flag, default value is true

    public static int getWindowsWidth(Context context) {
        WindowManager wm = (WindowManager)context.getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getWidth();
    }


    public static int getWindowsHeight(Context context) {
        WindowManager wm = (WindowManager)context.getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return display.getHeight();
    }

    public static int getStatusBarHeight(Context context) {
        int height = (int)Math.ceil(25 * context.getResources().getDisplayMetrics().density);
        return height;
    }

    public static void LogD(String log) {
        if (DEBUG) {
            Log.d(TAG, log);
        }
    }

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
            e.printStackTrace();
        }
        return c; // returns null if camera is unavailable
    }

    public static boolean isHardwareSupportFlashLight(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            // this device has a camera
            return true;
        }
        // no camera on this device
        return false;
    }

    public static void decodeLampMessage(String text) {
        long[] pattern = MorseCodeConverter.pattern(text);
        // the first char of pattern is 0, we start from the second
        for (int i = 1; i < pattern.length; i++) {
            long code = pattern[i];
            if (code == MorseCodeConverter.DOT) {
                XUtils.LogD("DOT");
                shortFlash();
            } else if (code == MorseCodeConverter.DASH) {
                XUtils.LogD("DASH");
                longFlash();
            } else if (code == MorseCodeConverter.GAP) {
                XUtils.LogD("GAP");
                //interval(BASE_INTERVAL);
            } else if (code == MorseCodeConverter.LETTER_GAP) {
                XUtils.LogD("LETTER_GAP");
                interval(1000);
            } else if (code == MorseCodeConverter.WORD_GAP) {
                XUtils.LogD("WORD_GAP");
                interval(2000);
            } else {
                XUtils.LogD("invalide charactor");
            }
        }
    }
    private synchronized static void shortFlash() {
        long start = System.currentTimeMillis();
        mCamera = XUtils.getCameraInstance();
        Camera.Parameters param = mCamera.getParameters();
        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(param);
        //mCamera.startPreview();
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // turn off
        Camera.Parameters paramOff = mCamera.getParameters();
        mCamera.stopPreview();
        paramOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(paramOff);
        //mCamera.startPreview();
        mCamera.release();
        long end = System.currentTimeMillis();
        XUtils.LogD("short flash:"+(end-start));
    }

    private synchronized static void longFlash() {
        long start = System.currentTimeMillis();
        mCamera = XUtils.getCameraInstance();
        Camera.Parameters param = mCamera.getParameters();
        param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(param);
        //mCamera.startPreview();
        try {
            Thread.sleep(400);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // turn off
        Camera.Parameters paramOff = mCamera.getParameters();
        mCamera.stopPreview();
        paramOff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(paramOff);
        //mCamera.startPreview();
        mCamera.release();
        long end = System.currentTimeMillis();
        XUtils.LogD("long flash:"+(end-start));
    }

    private static void interval(long interval) {
        try {
            Thread.sleep(interval);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void vibrate(Context context, long time) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);  
        vibrator.vibrate(time);  
    }
}
